package com.ruoyi.business.services;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.protocol.IDynamicSearchService;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Map;

@Service
public class HttpService {

    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);


    @Autowired
    private IDynamicSearchService dynamicSearchService;

    private final String REQ_RESULT = "req_result";

    @Async
    public void sendPost(String url, String param, String ip, String ua, String uuid, String reqType){
        String res = HttpUtils.sendPost(url, param, ip, ua);

        JSONObject esJson = new JSONObject();
        esJson.put("id", uuid);
        esJson.put("url", url);
        esJson.put("param", param);
        esJson.put("reqType", reqType);
        esJson.put("reportResultsStr", res);
        esJson.put("createTime", DateUtils.getTime());
        dynamicSearchService.createDocument(esJson, REQ_RESULT, uuid);
    }


    @Async
    public void sendPost(String url, String paramJsonStr, Map<String, String> headers, String uuid, String reqType){
        String res = HttpUtil.createPost(url).body(paramJsonStr).addHeaders(headers).execute().body();
        System.out.println("url: " +url);
        System.out.println("res: "+ res);
        JSONObject esJson = new JSONObject();
        esJson.put("id", uuid);
        esJson.put("url", url);
        esJson.put("param", paramJsonStr);
        esJson.put("reqType", reqType);
        esJson.put("reportResultsStr", res);
        esJson.put("createTime", DateUtils.getTime());
        dynamicSearchService.createDocument(esJson, REQ_RESULT, uuid);
    }






    @Async
    public void sendGet(String url, String param, String ip, String ua, String uuid, String reqType){
        String res = HttpUtils.sendGet(url, param, ip, ua);

        JSONObject esJson = new JSONObject();
        esJson.put("id", uuid);
        esJson.put("url", url);
        esJson.put("param", param);
        esJson.put("reqType", reqType);
        esJson.put("reportResultsStr", res);
        esJson.put("createTime", DateUtils.getTime());
        dynamicSearchService.createDocument(esJson, REQ_RESULT, uuid);
    }
}
